<?

function forum_get_update_lastusers($thread_id,$author=""){
	global $SETTINGS;
	global $db_pref;

	$cou=$SETTINGS['count_authors_on_thread'];

	$reply_count=dbcount("(post_id)", $db_pref."forum_posts", "thread_id='".$thread_id."'");
		
	if($reply_count==0 && $reply_count==1){
		return $author;
	}elseif($reply_count<=$cou){
		$cou=$reply_count-1;
	}

	$auths='';
	$result = dbquery("SELECT fp.* FROM ".$db_pref."forum_posts fp
		WHERE fp.thread_id='$thread_id' ORDER BY fp.post_id DESC LIMIT 0,".($cou));
	if(dbrows($result)!=0) {
		$postu=sql_array($result);
		$auths="";
		for($i=count($postu)-1; $i>=0; $i--){
			$auths.=($auths ? ", " : "");
			$auths.=$postu[$i]['post_author'];
		}
		if($author==""){
			
		}else{
			$auths.=($auths ? ", " : "");
		}
	}
	$auths.=$author;

	return $auths;
}

function forum_update_lastusers($thread_id,$author=""){
	global $db_pref;
	$autr=forum_get_update_lastusers($thread_id,$author);
	sql_update($db_pref."forum_threads",array("thread_posting"),array($autr),"thread_id='$thread_id'");
}

function forum_get_preview($text){
	global $SETTINGS;
	$pretext=substr($text, 0, $SETTINGS['max_text_on_thread_prev']);
	if(strlen($text)>strlen($pretext)){
		$pretext.="...";
	}
	$pretext=str_replace( "\n", " " , $pretext);
	$pretext=str_replace( "\r", "" , $pretext);
	return wordwrap_preview($pretext);	
}

function forum_set_update_preview($thread_id,$text=""){
	global $db_pref;
	sql_update($db_pref."forum_threads",array("thread_preview"),array($text),"thread_id='$thread_id'");
}

function forum_update_first_post_from_thread($thread_id){
	global $db_pref;
	$result=dbquery("SELECT post_message,post_user_id,post_author FROM ".$db_pref."forum_posts WHERE thread_id='$thread_id' ORDER BY post_id ASC LIMIT 0,1");
	if(dbrows($result)){
		$psdata2=dbarray($result);
		$perv=forum_get_preview($psdata2['post_message']);
		sql_update($db_pref."forum_threads",array("thread_author","thread_user_id","thread_preview"),array($psdata2['post_author'],$psdata2['post_user_id'],$perv),"thread_id='$thread_id'");
	}
}

function forum_move_th($thread_id,$new_forum_id){
	global $db_pref;
	
	$result=dbquery("SELECT * FROM ".$db_pref."forums WHERE forum_id='$new_forum_id'");
	if(dbrows($result)){ 
		$pdata2=dbarray($result);

		$result = dbquery("SELECT * FROM ".$db_pref."forum_threads WHERE thread_id='$thread_id'");
		if(dbrows($result)){ 
			$pdata=dbarray($result);

			if($pdata['forum_id']!==$pdata2['forum_id'] && $pdata2['forum_cat']!==0){
				sql_update($db_pref."forum_threads",array("forum_id"),array($pdata2['forum_id']),"thread_id='$thread_id'");
				sql_update($db_pref."forum_posts",array("forum_id"),array($pdata2['forum_id']),"thread_id='$thread_id'");
			}	
		}
	}

}

?>